package com.wyman.context.support;

import com.wyman.BeanException;
import com.wyman.context.ConfigurableApplicationContext;
import com.wyman.core.io.DefaultResourceLoader;
import com.wyman.factory.ConfigurableListableBeanFactory;
import com.wyman.factory.config.BeanFactoryPostProcessor;
import com.wyman.factory.config.BeanPostProcessor;

import java.util.Map;

/**
 * @Author 李伟民
 * @Description
 * @DateTime 2024/12/12 00:54
 */
public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {


    //模板设计模式 规定实现流程 但对每一步不做具体实现
    @Override
    public void refresh() throws BeanException {
        //创建bean工厂 并且加载bean definition
        refreshBeanFactory();
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();

        //12.14 8在执行bean前置处理方法之前 将定义的负责设置application context的aware processor添加到factory中
        beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

        //初始化之前修改bean factory中的 bean definition
        invokeBeanFactoryPostProcessors(beanFactory);
        //需要在创建好bean  添加到registry之前 分别在初始化前后执行处理器的方法 处理创建出来的bean对象
        //这里是将处理器添加进去 以供apply方法调用
        registerBeanPostProcessors(beanFactory);
        //提前实例化单例Bean对象
        beanFactory.preInstantiateSingletons();
    }

    //创建bean factory 同时加载bean definition信息
    public abstract void refreshBeanFactory() throws BeanException;

    public abstract ConfigurableListableBeanFactory getBeanFactory();

    private void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory){
        Map<String,BeanFactoryPostProcessor>  beanFactoryPostProcessorMap = beanFactory.getBeansOfType(BeanFactoryPostProcessor.class);
        for(BeanFactoryPostProcessor beanFactoryPostProcessor:beanFactoryPostProcessorMap.values()){
            beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
        }
    }

    private void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory){
        Map<String, BeanPostProcessor> beanPostProcessorMap = beanFactory.getBeansOfType(BeanPostProcessor.class);
        for(BeanPostProcessor beanPostProcessor:beanPostProcessorMap.values()){
            beanFactory.addBeanPostProcessor(beanPostProcessor);
        }
    }

    //后面所以得功能 都是通过beanFactory的方法实现 只不过包装到了上下文中 用户直接调用上下文即可
    @Override
    public <T> Map<String,T> getBeansOfType(Class<T> type) throws BeanException{
        return getBeanFactory().getBeansOfType(type);
    }

    @Override
    public String[] getBeanDefinitionNames(){
        return getBeanFactory().getBeanDefinitionNames();
    }

    @Override
    public Object getBean(String beanName) throws BeanException{
        return getBeanFactory().getBean(beanName);
    }

    @Override
    public Object getBean(String name,Object[] args){
        try {
            return getBeanFactory().getBean(name,args);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public <T> T getBean(String name,Class<T> type){
        try {
            return (T)getBeanFactory().getBean(name,type);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //注册关闭钩子 我的理解是虚拟机关闭时 会启动一个线程 线程会执行close方法
    @Override
    public void registerShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread(
                ()->{
                    close();
                }
        ));
    }

    //手动关闭
    @Override
    public void close() {
        getBeanFactory().destroySingletons();
    }
}
